package hu.vissy.yacap.option;

import hu.vissy.yacap.exception.CommandLineParserException;

/**
 * 4-byte signed integer option.
 *
 * @author balage
 *
 */
public class IntegerOption extends CommandLineOption<Integer> {

    public static class Builder extends CommandLineOptionBuilder<Integer, Builder> {
        public Builder(String longForm) {
            super(Integer.class, longForm);
        }

        /**
         * Uses covariant return type to precast result.
         *
         */
        @Override
        public IntegerOption getInstance() {
            return (IntegerOption) super.getInstance();
        }

        @Override
        protected CommandLineOption<Integer> createInstance() {
            return new IntegerOption(longForm);
        }

        @Override
        protected Builder getThis() {
            return this;
        }
    }


    /**
     * Constructor.
     *
     * @param longForm
     *            The long name of the command.
     */
    public IntegerOption(String longForm) {
        super(Integer.class, longForm);
    }

    @Override
    public Integer parseValue(String valueString) throws CommandLineParserException {
        try {
            return Integer.parseInt(valueString);
        } catch (NumberFormatException e) {
            throw new CommandLineParserException("Invalid integer value: " + valueString + " (Option: " + getLongForm() + ")");
        }
    }



}
